﻿using System;
using AccessControl.Contracts.Models;
using AccessControl.Contracts.Settings;
using System.Collections.Generic;
using AccessControl.Data.Models;
using System.Windows.Input;
using AccessControl.Wpf.Commands;
using System.IO;

namespace AccessControl.Wpf.Settings.ViewModels
{
	class MapsSettingsViewModel : SettingsBaseViewModel<IMap>
	{
		#region Fields

		List<IEntry> _EntryRoots = new List<IEntry>();

		#endregion

		#region Public Constructor

		public MapsSettingsViewModel(ISettingsService settingsService)
			: base(settingsService.Maps)
		{
			Name = "Maps";
			_EntryRoots.Add(new Entry());
		}

		#endregion

		#region Public Properties

		public IList<IEntry> EntryRoots
		{
			get { return _EntryRoots; }
		}

		#endregion

		#region SettingsBaseViewModel Overrides

		public override void AddItem(IMap item)
		{
			SettingsService.Add(item);
		}

		public override void EditItem(IMap item)
		{
			SettingsService.Update(item);
		}

		public override void DeleteItem(IMap item)
		{
			SettingsService.Delete(item);
		}

		public override bool IsValidItem(IMap item)
		{
			return true;
		}

		public override IMap CreateNewItem()
		{
			/* Now create the new map item */
			return new Map();
		}

		public override void LoadSettings()
		{
			/* First the base class */
			base.LoadSettings();
			
			/* And select the first item */
			if (SettingsService.Items.Count > 0)
			{
				CurrentItem = SettingsService.Items[0];
				/* And we in edit mode now */
				CurrentState = SettingsState.Editing;
			}
		}

		#endregion

		#region Commands

		public ICommand LoadNewMapCommand
		{
			get { return new CommandBase(LoadNewMap); }
		}

		#endregion

		#region Private Members

		private void LoadNewMap()
		{
			FileStream stream;
			byte[] imageBytes;

			/* If the current item is not null delete it */
			if (CurrentItem != null)
				SettingsService.Delete(CurrentItem);


			Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Filter = "Image Files|*.bmp;*.jpg;*.png";
			openFileDialog.Multiselect = false;
			bool? res = openFileDialog.ShowDialog();
			if (res.HasValue && res.Value)
			{
				string sFileName = openFileDialog.FileName;

				/* Open the file for reading */
				try { stream = new FileStream(sFileName, FileMode.Open); }
				catch
				{
					MessagingService.Show("Could not load the image", "AccessControl", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
					return;
				}

				/* Read the bytes */
				try
				{
					imageBytes = new byte[stream.Length];
					stream.Read(imageBytes, 0, imageBytes.Length);
				}
				catch
				{
					MessagingService.Show("Could not load the image", "AccessControl", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
					return;
				}
				finally
				{
					stream.Close();
				}

				/* Now create the new map item */
				CurrentItem = new Map()
				{
					MapImageBytes = imageBytes
				};

				/* And set adding mode */
				CurrentState = SettingsState.Adding;
			}
		}

		#endregion
	}
}
